package app.web.cartoon.report.word.tool;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.db.sql.DBManager;
import cn.lily.com.lang.BusinessDate;

/**
 * 动漫首字母统计
 * @author JiaJ
 *
 */
public class CartoonWordReport {
	
	private Map<String,Integer> totalMap = new HashMap<String,Integer>();
	
	private Map<String,Integer> detailMap = new HashMap<String,Integer>();
	
	private DataTable dataTable;
	
	private void instData(){
		
		DBManager dbManager = DBFactory.getActionDB();
		
		String sql = "select count(*) TOTAL,CAN_FIRST from LEO_CAN_INFO group by CAN_FIRST";		
		this.dataTable = dbManager.executeQueryTable(sql);		
		while(this.dataTable.next()){		
			this.totalMap.put(this.dataTable.getString("CAN_FIRST").trim().toUpperCase(), this.dataTable.getInt("TOTAL"));
		}
		
		sql = "select count(*) TOTAL,canTotal.CAN_FIRST "
			+ "from LEO_CAN_INFO canTotal "
			+ "inner join LEO_CAN_DETAIL_INFO canDetail on canTotal.CAN_ID=canDetail.CAN_ID "
			+ "group by canTotal.CAN_FIRST ";
		this.dataTable = dbManager.executeQueryTable(sql);		
		while(this.dataTable.next()){		
			this.detailMap.put(this.dataTable.getString("CAN_FIRST").trim().toUpperCase(), this.dataTable.getInt("TOTAL"));
		}
		
	}
	
	private void insertData(){
		
		DBFactory.getActionDB().executeUpdate("delete from LEO_CAN_REPORT_WORD");
		
		Set<String> firsts = this.totalMap.keySet();
		for(String first:firsts){
			
			int total = this.totalMap.get(first);
			int detail = this.detailMap.get(first);
			String date = BusinessDate.getToday();
			
			String sql = " insert into LEO_CAN_REPORT_WORD (CAN_FIRST,CAN_BASE_NUM,CAN_DETAIL_NUM,CAN_REP_DATE,CAN_REP_OPER) "
			           + " values ('"+first+"',"+total+","+detail+",to_date('"+date+"','yyyy-mm-dd'),'9999')";
	
			DBFactory.getActionDB().executeUpdate(sql);
			
		}
		
		DBFactory.getActionDB().commit();
		
	}
	
	/**
	 * 执行
	 */
	public void inst(){
		this.instData();
		this.insertData();
		this.free();
	}
	
	/**
	 * 释放缓存
	 */
	public void free(){
		this.totalMap.clear();
		this.detailMap.clear();
	}
	
}
